<template>
  <div v-if="visible" class="message-notification" @click="handleClick">
    <div class="notification-content">
      <div class="notification-icon">
        <el-icon><ChatDotRound /></el-icon>
      </div>
      <div class="notification-text">
        <div class="notification-title">新消息提醒</div>
        <div class="notification-message">
          {{ senderName }} 给您发送了 {{ messageCount }} 条新消息
        </div>
      </div>
      <div class="notification-close" @click.stop="close">
        <el-icon><Close /></el-icon>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import { ChatDotRound, Close } from '@element-plus/icons-vue'

export default {
  name: 'MessageNotification',
  components: {
    ChatDotRound,
    Close
  },
  props: {
    senderName: {
      type: String,
      required: true
    },
    messageCount: {
      type: Number,
      default: 1
    },
    autoClose: {
      type: Number,
      default: 5000 // 5秒后自动关闭
    }
  },
  emits: ['click', 'close'],
  setup(props, { emit }) {
    const visible = ref(true)
    let autoCloseTimer = null

    const handleClick = () => {
      emit('click')
      close()
    }

    const close = () => {
      visible.value = false
      emit('close')
      if (autoCloseTimer) {
        clearTimeout(autoCloseTimer)
      }
    }

    onMounted(() => {
      if (props.autoClose > 0) {
        autoCloseTimer = setTimeout(() => {
          close()
        }, props.autoClose)
      }
    })

    onUnmounted(() => {
      if (autoCloseTimer) {
        clearTimeout(autoCloseTimer)
      }
    })

    return {
      visible,
      handleClick,
      close
    }
  }
}
</script>

<style scoped>
.message-notification {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  cursor: pointer;
  animation: slideInRight 0.3s ease-out;
}

.notification-content {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  border: 1px solid #e4e7ed;
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 300px;
  max-width: 400px;
  transition: all 0.3s ease;
}

.notification-content:hover {
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.notification-icon {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  flex-shrink: 0;
}

.notification-text {
  flex: 1;
  min-width: 0;
}

.notification-title {
  font-weight: 600;
  color: #303133;
  font-size: 14px;
  margin-bottom: 4px;
}

.notification-message {
  color: #606266;
  font-size: 13px;
  line-height: 1.4;
  word-wrap: break-word;
}

.notification-close {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  cursor: pointer;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.notification-close:hover {
  background: #f5f7fa;
  color: #606266;
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(100%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .message-notification {
    top: 10px;
    right: 10px;
    left: 10px;
  }
  
  .notification-content {
    min-width: auto;
    max-width: none;
  }
}
</style>